 #include "CNotePattern.h"
#include "CFife.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "CXInput.h"

CNotePattern::CNotePattern()
{
	m_szNotePattern.clear();
	Init();

	m_n360ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/360FifeNotes.png");
	m_n360AltImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/360FifeNotes_Alt.png");
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/FifeNotes.png");
	m_nAltImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/FifeNotes_Alt.png");
}

CNotePattern::~CNotePattern()
{

}

void CNotePattern::Init()
{
	m_szNotePattern.clear();
	int one = rand() % ( 100 - 1 + 1 ) + 1;
	if(one < 25)
		AddNote('0');
	else if(one < 50)
		AddNote('1');
	else if(one < 75)
		AddNote('2');
	else if(one <= 100)
		AddNote('3');

	int two = rand() % ( 100 - 1 + 1 ) + 1;
	if(two < 25)
		AddNote('0');
	else if(two < 50)
		AddNote('1');
	else if(two < 75)
		AddNote('2');
	else if(two <= 100)
		AddNote('3');

	if(m_szNotePattern[0] == m_szNotePattern[1])
		Init();
}

void CNotePattern::AddNote(char cNote)
{
	m_szNotePattern.push_back(cNote);
	if(m_szNotePattern.size() > 2)
		m_szNotePattern.erase(m_szNotePattern.begin());
}

void CNotePattern::Clear()
{
	m_szNotePattern.clear();
}

bool CNotePattern::Listen()
{
	if(!strcmp(CFife::GetInstance()->NoteStack().c_str(), m_szNotePattern.c_str()))
	{
		m_bFirst = m_bLast = true;
		return true;
	}
	else if(!strcmp(CFife::GetInstance()->NoteStack().c_str(), const_cast<char*>(&m_szNotePattern[0])))
		m_bFirst = true;
	else
		m_bFirst = false;

	m_bLast = false;

	return false;
}

void CNotePattern::Render(float fPosX, float fPosY)
{
	////////////////////////////////////////////
	//	Render First Note
	int one;
	int two;
	if(CXInput::GetInstance()->IsConnected())
	{
		if(m_bFirst)
			one = m_n360ImageID;
		else
			one = m_n360AltImageID;

		if(m_bLast)
			two = m_n360ImageID;
		else
			two = m_n360AltImageID;
	}
	else
	{
		if(m_bFirst)
			one = m_nImageID;
		else
			one = m_nAltImageID;

		if(m_bLast)
			two = m_nImageID;
		else
			two = m_nAltImageID;
	}

	RECT temp;
	temp.top = 0;
	temp.left = 0;
	temp.right = 32;
	temp.bottom = 32;

	switch(m_szNotePattern[0])
	{
	case '0':
		CSGD_TextureManager::GetInstance()->Draw(one, (int)fPosX, (int)fPosY, .5f,.5f,&temp);
		break;
	case '1':
		temp.left = 32;
		temp.right = 64;
		CSGD_TextureManager::GetInstance()->Draw(one, (int)fPosX, (int)fPosY, .5f,.5f,&temp);
		break;
	case '2':
		temp.left = 64;
		temp.right = 96;
		CSGD_TextureManager::GetInstance()->Draw(one, (int)fPosX, (int)fPosY, .5f,.5f,&temp);
		break;
	case '3':
		temp.left = 96;
		temp.right = 128;
		CSGD_TextureManager::GetInstance()->Draw(one, (int)fPosX, (int)fPosY, .5f,.5f,&temp);
		break;
	}
	temp.left = 0;
	temp.right = 32;
	//	Render Last Note
	switch(m_szNotePattern[1])
	{
	case '0':
		CSGD_TextureManager::GetInstance()->Draw(two, (int)fPosX+16, (int)fPosY, .5f,.5f,&temp);
		break;
	case '1':
		temp.left = 32;
		temp.right = 64;
		CSGD_TextureManager::GetInstance()->Draw(two, (int)fPosX+16, (int)fPosY, .5f,.5f,&temp);
		break;
	case '2':
		temp.left = 64;
		temp.right = 96;
		CSGD_TextureManager::GetInstance()->Draw(two, (int)fPosX+16, (int)fPosY, .5f,.5f,&temp);
		break;
	case '3':
		temp.left = 96;
		temp.right = 128;
		CSGD_TextureManager::GetInstance()->Draw(two, (int)fPosX+16, (int)fPosY, .5f,.5f,&temp);
		break;
	}
	////////////////////////////////////////////

}